#include "host_cmd.h"
#include "SC_Init.h"
#include "stdio.h"
#include "string.h"
#include "toolkit.h"
#include "SMProcess.h"
#include "drv_rgb.h"

#define HOST_CMD_HEAD 0X12
#define HOST_CMD_RECV_BUF_SIZE_MAX 30

#define HOST_CMD_CONTROL_EVENT 0X05
#define HOST_CMD_SET_VALUE 0X06
#define HOST_CMD_FAN_SPPED 0X07
#define HOST_CMD_RGB_CODE 0X08

uint16_t xdata pM2D5DebugValue = 0;
uint8_t xdata pm2d5DebugFlag = 0;

uint8_t xdata fanDebugValue = 0;
uint8_t xdata fanDebugFlag = 0;

uint8_t xdata rgbRedDebugValue = 0;
uint8_t xdata rgbGreenDebugValue = 0;
uint8_t xdata rgbBlueDebugValue = 0;
uint8_t xdata rgbDebugFlag = 0;
// typedef struct{
//

//}host_cmd_info_t;

static uint8_t xdata rcvStatCmd = 0x01;
static uint8_t xdata recvLengthCmd = 0;

extern bit Uart1SendFlag;
extern bit Uart1ReceiveFlag;
extern bit UartXReceiveFlag;
extern bit Uart0ReceiveFlag;
extern uint32_t xdata eventValue;

uint8_t xdata cmdRcv[HOST_CMD_RECV_BUF_SIZE_MAX] = {0};

void host_cmd_control_func(uint8_t rev_buf);
void host_cmd_set_pm2d5_func(uint8_t* value);
void host_cmd_set_fan_func(uint8_t value);
void host_cmd_set_rgb_func(uint8_t* value);

void cmd_receive_data(uint8_t recv)
{
	if (rcvStatCmd == 0x01)
	{
		if (recv == HOST_CMD_HEAD)
		{
			cmdRcv[0] = recv;
			rcvStatCmd = 0x02;
		}
	}
	else if (rcvStatCmd == 0x02)
	{
		cmdRcv[1] = recv;
		rcvStatCmd = 0x03;
		recvLengthCmd = 0;
	}
	else if (rcvStatCmd == 0x03)
	{
		cmdRcv[2 + recvLengthCmd] = recv;
		recvLengthCmd++;
		if (recvLengthCmd >= cmdRcv[1])
			rcvStatCmd = 0x04;
	}
	else if (rcvStatCmd == 0x04)
	{
		cmdRcv[2 + recvLengthCmd] = recv;
		rcvStatCmd = 0x05;
	}
}

void host_cmd_data_handle(void)
{
	uint32_t i, sum;
	if (rcvStatCmd == 0x05) //
	{
		sum = 0;
		for (i = 0; i <= recvLengthCmd + 2; i++)
		{
			sum += cmdRcv[i];
			sum &= 0x00ff;
		}
		sum = 0xFF;
		if (sum == 0xFF)		//sys
		{
			if (cmdRcv[2] == HOST_CMD_CONTROL_EVENT)
			{
				printf("host cmd key event:%bu\r\n", cmdRcv[3]);
				host_cmd_control_func(cmdRcv[3]);
			}else
			if (cmdRcv[2] == HOST_CMD_SET_VALUE)
			{
				//printf("host cmd pm2d5 value set:%bu\r\n", cmdRcv[3]*256 + cmdRcv[4]);
				host_cmd_set_pm2d5_func(&cmdRcv[3]);
			}
			else if (cmdRcv[2] == HOST_CMD_FAN_SPPED)
			{
				printf("host cmd fan duty:%bu\r\n", cmdRcv[3]);
				host_cmd_set_fan_func(cmdRcv[3]);
			}
			else if (cmdRcv[2] == HOST_CMD_RGB_CODE)
			{
				printf("host cmd rgb set:R%bu G%bu B%bu\r\n", cmdRcv[3], cmdRcv[4], cmdRcv[5]);
				host_cmd_set_rgb_func(&cmdRcv[3]);
			}
		}
		else
		{
			memset(cmdRcv, 0x00, sizeof(cmdRcv));
			printf("host cmd checksum failed\r\n");
		}

		rcvStatCmd = 0x01;
	}
	else if (rcvStatCmd == 0x06)
	{
		memset(cmdRcv, 0x00, sizeof(cmdRcv));
		rcvStatCmd = 0x01;
		printf("host cmd receive failed:%d\r\n", rcvStatCmd);
	}
}

void host_cmd_control_func(uint8_t rev_buf)
{
	if (rev_buf == 0x01)
	{
		eventValue = CTR_ONOFF_EVENT;
	}
	else if (rev_buf == 0x02)
	{
		//eventValue = CTR_TIM_EVENT;
	}
	else if (rev_buf == 0x03)
	{
		eventValue = CTR_MODE_EVENT;
	}
	else if (rev_buf == 0x04)
	{
		eventValue = CTR_AMD_EVENT;
	}
	else
	{
		printf("host cmd control failed\r\n");
	}
}

void host_cmd_set_pm2d5_func(uint8_t* value)
{
	pM2D5DebugValue = value[0]*256 + value[1];
	
	if(pM2D5DebugValue > 999){
		pm2d5DebugFlag = 0x0;
		printf("host cmd pm2d5 debug off\r\n", pM2D5DebugValue);
	}		
	else{
		pm2d5DebugFlag = 0xaa;
		printf("host cmd pm2d5 value set:%d\r\n", pM2D5DebugValue);	
	}

}

void host_cmd_set_fan_func(uint8_t value)
{
	if(value > 100){
		fanDebugFlag = 0x0;
		fanDebugValue = 0;
	}else{
		fanDebugFlag = 0xaa;
		fanDebugValue = value;	
		fan_set_value(value);
	}
}

void host_cmd_set_rgb_func(uint8_t* value)
{
	rgbledSetCode(value[0], value[1], value[2]);
}

